package com.eagle.view.activity.base;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import com.eagle.EApplication;
import com.eagle.support.common.MessageHelper;
import com.eagle.support.constants.Constants;
import com.eagle.support.helper.AccountHelper;
import com.eagle.support.helper.CallHelper;
import com.eagle.view.activity.CallDialingActivity_;
import com.kinstalk.voip.sdk.EngineSdkCallState;
import com.kinstalk.voip.sdk.EngineSdkVideoPixelFormat;
import com.kinstalk.voip.sdk.logic.sip.aidl.model.CallInfo;

import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Receiver;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;

/**
 * Created by gc on 2016/12/6. Base activity
 */
@RuntimePermissions
@EActivity
public abstract class BaseCallActivity extends BaseActivity implements CallHelper.IDataResultListener,
        CallHelper.ICallStateListener, AccountHelper.IUserStateListener, CallHelper.IMeetingStateListener {

    /**
     * 直播操作类
     */
    protected CallHelper mCallHelper;

    /**
     * 账号操作类
     */
    protected AccountHelper mAccountHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseCallActivityPermissionsDispatcher.initPermissionWithCheck(this);
        mCallHelper = CallHelper.getInstance();
        mAccountHelper = AccountHelper.getInstance();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mCallHelper.registerCallStateListener(this);
        mCallHelper.registerDataResultListener(this);
        mCallHelper.registerMeetingStateListener(this);
        mAccountHelper.registerUserStateListener(this);
    }

    @NeedsPermission({
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.MODIFY_AUDIO_SETTINGS,
            Manifest.permission.WAKE_LOCK,
            Manifest.permission.CALL_PHONE,
            Manifest.permission.CAMERA,
            Manifest.permission.USE_SIP
    })
    protected void initPermission() {
        if (!EApplication.mInitState) {
            EApplication.mInitState = true;
            MessageHelper.initApplication();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        BaseCallActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @Override
    public void onMessageSendResult(String roomId) {
        Log.e("cim", "BaseCallActivity onMessageSendResult: " + roomId);
    }

    @Override
    public void onPushAddCid(String cid) {
        Log.e("cim", "BaseCallActivity onPushAddCid: " + cid);
    }

    @Override
    public void onPushByeCid(String cid) {
        Log.e("cim", "BaseCallActivity onPushByeCid: " + cid);
    }

    @Override
    public void onMainVideoDataInCall(long callToken, long pointer, int width, int height, char rotation, EngineSdkVideoPixelFormat format, long channelId) {

    }

    @Override
    public void onCallStateHandler(CallInfo call, int state) {
        String uid = EApplication.getSharedPreferences().getString(Constants.LOGIN_UID, "");
        if (EngineSdkCallState.swigToEnum(state) == EngineSdkCallState.ES_STATE_ON_CALL_INCOMMING_CALL && !TextUtils.isEmpty(uid)) {
            transitionTo(CallDialingActivity_.intent(BaseCallActivity.this).
                    extra(Constants.CALL_TYPE, Constants.CALL_TYPE_INCOMING).
                    extra(Constants.CALL_UID, call.getCallConfiguration().getRemoteAccountId()).
                    get());
        }
    }

    @Override
    public void onUserStateHandler(int state) {

    }

    public CallHelper getCallHelper() {
        return mCallHelper;
    }

    protected void finishPage() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            finishAfterTransition();
        } else {
            finish();
        }
    }

    /**
     * 收到邀请
     */
    @Override
    public void onGotInviteResult(String roomId, String sender) {
        String uid = EApplication.getSharedPreferences().getString(Constants.LOGIN_UID, "");
        if (!TextUtils.isEmpty(sender) && !TextUtils.isEmpty(uid)) {
            transitionTo(CallDialingActivity_.intent(BaseCallActivity.this).
                    extra(Constants.CALL_TYPE, Constants.CALL_TYPE_MEETING_INVITE).
                    extra(Constants.ROOM_ID, roomId).
                    extra(Constants.MEETING_SENDER, sender).
                    get());
        }
    }

    @Override
    public void onGotMeetingRoomResult(String roomId) {

    }

    /**
     * 流量监听
     */
    @Receiver(actions = Constants.ACTION_NET_WORK_SPEED, registerAt = Receiver.RegisterAt.OnCreateOnDestroy)
    protected void receiverSpeed(@Receiver.Extra("rx") String rx, @Receiver.Extra("tx") String tx) {

    }

    /**
     * 退出直播窗口
     */
    public void popQuitLiveDialog() {

    }

    /**
     * 文字消息输入
     */
    public void handlerMessageInput(boolean show) {

    }

    /**
     * 连麦状态
     */
    public void refreshConnectState(boolean connected) {

    }

    /**
     * 关闭预览
     */
    public void hideCamera(boolean b) {

    }

    /**
     * 切换镜头
     */
    public void switchCamera() {

    }
}
